﻿using System.Collections.Generic;
using MTC.Web.Business.DTO;
using Owner = MTC.Business.Owner.Owner;

namespace MTC.Web.Business
{
    public class Owners
    {
        private List<OwnerEntity> _ownerEntityList = new List<OwnerEntity>();
        
        public List<OwnerEntity> GetOwners()
        {
            List<MTC.Common.DTO.Owner> owners;
            owners = Owner.GetOwners();
            if (owners == null)
                return null;
            _ownerEntityList.Clear();
            foreach (MTC.Common.DTO.Owner owner in owners)
            {
                OwnerEntity entity = new OwnerEntity();
                entity.OwnerId = owner.OwnerId;
                entity.ABN = owner.ABN;
                entity.CompanyName = owner.CompanyName;
                entity.FirstName = owner.FirstName;
                entity.LastName = owner.LastName;
                entity.Mobile = owner.Mobile;
                entity.Phone = owner.Phone;
                entity.Postcode = owner.Postcode;
                entity.State = owner.State;
                entity.Street1 = owner.Street1;
                entity.Street2 = owner.Street2;
                entity.Suburb = owner.Suburb;
                entity.Title = owner.Title;
                _ownerEntityList.Add(entity);
            }
            return _ownerEntityList;
        }

        public OwnerEntity GetAOwner(int ownerid )
        {
            if (ownerid == 0)
                return null;
            OwnerEntity entity = null;
            MTC.Common.DTO.Owner owner = Owner.GetOwner(ownerid);
            if (owner != null)
            {
                entity = new OwnerEntity();
                entity.OwnerId = owner.OwnerId;
                entity.ABN = owner.ABN;
                entity.CompanyName = owner.CompanyName;
                entity.FirstName = owner.FirstName;
                entity.LastName = owner.LastName;
                entity.Mobile = owner.Mobile;
                entity.Phone = owner.Phone;
                entity.Postcode = owner.Postcode;
                entity.State = owner.State;
                entity.Street1 = owner.Street1;
                entity.Street2 = owner.Street2;
                entity.Suburb = owner.Suburb;
                entity.Title = owner.Title;
              
            }
            return entity;
        }

        public bool UpdateAOwner(OwnerEntity entity)
        {
            if (entity==null)
            {
                return false;
            }
            MTC.Common.DTO.Owner owner = new Common.DTO.Owner();

            owner.OwnerId = entity.OwnerId;
            owner.ABN = entity.ABN;
            owner.CompanyName = entity.CompanyName;
            owner.FirstName = entity.FirstName;
            owner.LastName = entity.LastName;
            owner.Mobile = entity.Mobile;
            owner.Phone = entity.Phone;
            owner.Postcode = entity.Postcode;
            owner.State = entity.State;
            owner.Street1 = entity.Street1;
            owner.Street2 = entity.Street2;
            owner.Suburb = entity.Suburb;
            owner.Title = entity.Title;

            return Owner.UpdateOwner(owner);

        }

        public bool DeleteAOwner(int OwnerId)
        {
            if (OwnerId == 0)
                return false;

            return Owner.DeleteOwner(OwnerId);
        }

        public int AddOwner(OwnerEntity entity)
        {
            if (entity == null)
            {
                return 0;
            }
            MTC.Common.DTO.Owner owner = new Common.DTO.Owner();

            owner.OwnerId = entity.OwnerId;
            owner.ABN = entity.ABN;
            owner.LastName = entity.LastName;
            owner.CompanyName = entity.CompanyName;
            owner.FirstName = entity.FirstName;
            owner.Mobile = entity.Mobile;
            owner.Phone = entity.Phone;
            owner.Postcode = entity.Postcode;
            owner.State = entity.State;
            owner.Street1 = entity.Street1;
            owner.Street2 = entity.Street2;
            owner.Suburb = entity.Suburb;
            owner.Title = entity.Title;

            return Owner.AddOwner(owner);
        }
    }
}
